// pages/login/SignIn/index.js
import storage from "../../../utils/storage.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ windowHeight: res.windowHeight })
      }
    })
    that.setData({ options })
  },
  // 登陆
  bindgetuserinfo(e) {
    let that = this
    var parent = storage.getStorage('theElderlyId') || ''
    var gymnasium_id = storage.getStorage('gymnasium_id') || ''
    if (e.detail.userInfo) {
      wx.login({
        success: function (res) {
          var code = res.code
          wx.request({
            url: 'https://www.yuehanme.com/api/v1/User/login?code=' + code + '&parent=' + parent + '&gymnasium_id=' + gymnasium_id,
            success: function (res) {
              if (res.data.data.is_new == false) {
                storage.setStorage('is_new', '1')
              } else if (res.data.data.is_new == true) {
                storage.setStorage('is_new', '0')
              }

              storage.setStorage('token', res.data.data.token)
              storage.setStorage('user_id', res.data.data.user_id)
              storage.setStorage('mobile', res.data.data.mobile)
              wx.getUserInfo({
                // mobile
                success: function (res) {
                  var nickname = res.userInfo.nickName
                  var avatar = res.userInfo.avatarUrl
                  var ajax = app.myRequest()
                  ajax.getRequest('/User/auth', { nickname, avatar }).then((res) => {
                    wx.navigateBack()
                    wx.setStorageSync('is_guide', "1")
                  })
                }
              })
            }
          })
        }
      })
    }
  },
  // 获取用户信息
  getUserInfo(e) {
    console.log('用户信息', e)
  },
  PhoneNumber(e) {
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数。
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})